package com.socene.kotlin.config

import com.alibaba.fastjson.serializer.SerializerFeature
import com.alibaba.fastjson.support.config.FastJsonConfig
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import java.nio.charset.Charset


/**
 *Created by: Sorata 2017/9/9 下午4:18
 *
 */
@Configuration
class WebConfig {

    @Bean(name = arrayOf("fastJsonHttpMessageConverter4"))
    fun fastJsonHttpMessageConverter4(): FastJsonHttpMessageConverter4 {
        val converter = FastJsonHttpMessageConverter4()

        converter.supportedMediaTypes = arrayListOf(
                MediaType.APPLICATION_JSON_UTF8,
                MediaType(MediaType.TEXT_HTML, Charset.forName("UTF-8")),
                MediaType(MediaType.TEXT_PLAIN, Charset.forName("UTF-8")),
                MediaType(MediaType.APPLICATION_FORM_URLENCODED, Charset.forName("UTF-8")),
                MediaType.MULTIPART_FORM_DATA
        )

        val config = FastJsonConfig()
        config.charset = Charset.forName("UTF-8")
        config.setSerializerFeatures(
                SerializerFeature.PrettyFormat,
                // SerializerFeature.WriteClassName,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.WriteNullBooleanAsFalse,
                SerializerFeature.WriteNullNumberAsZero,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty
        )
     //   config.dateFormat = "yyyyMMddHHmmss"
        converter.fastJsonConfig = config
        return converter
    }
}


